package gui.advertiser;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import application.ArenaManager;

public class AdvertiserGUI extends JPanel {

	private JPanel centerPanel;
	private JTable table;
	private JTextField txtfIndex;
	//private ArrayList<Ad> ads = new ArrayList<Ad>();

	/**
	 * Create the frame.
	 * @param guiController 
	 */
	public AdvertiserGUI() {
		JLabel lblAdvertiser = new JLabel("Advertiser");
		centerPanel = new JPanel();

		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(new BorderLayout(0, 0));

		add(centerPanel, BorderLayout.CENTER);
		centerPanel.setLayout(new BorderLayout(0, 0));
	
		centerPanel.add(AdvertisementTable(), BorderLayout.CENTER);
	
		add(lblAdvertiser, BorderLayout.NORTH);
		addAdvertiserMenu();
		//add(initButtonPanel(), BorderLayout.SOUTH);
	}
	
	private JPanel AdvertisementTable() {
		JPanel tablePanel = new JPanel();
		//guiController.getAdvertisements();
		tablePanel.setBorder(BorderFactory.createTitledBorder("AdDatabase"));
		table = new JTable();
	
		table.setColumnSelectionAllowed(false);
		table.getModel().addTableModelListener(new TableModelListener() {
			
			@Override
			public void tableChanged(TableModelEvent e) {
				System.out.println(e.getColumn());
				
			}
		});
		table.setModel(new DefaultTableModel(initializeTable(), new String[] {"ID", "Banner Style", "Comment", "Status"}));
		table.getColumnModel().getColumn(0).setPreferredWidth(29);
		table.getColumnModel().getColumn(1).setPreferredWidth(110);
		table.getColumnModel().getColumn(2).setPreferredWidth(182);
		tablePanel.setLayout(new BorderLayout(0, 0));
		tablePanel.add(table);
		
		JPanel indexPanel = new JPanel();
		tablePanel.add(indexPanel, BorderLayout.SOUTH);
		
		txtfIndex = new JTextField();
		indexPanel.setBorder(BorderFactory.createTitledBorder("Enter ID:"));
		indexPanel.add(txtfIndex);
		txtfIndex.setColumns(10);
		return tablePanel;
	}


	private String[][] initializeTable() {
		addDummyData();
		String[][] rowsAndColumn = {
			{"1", "Bottom", "Description", "Active"},
			{"2", "Side", "Description2", "Active"},
			{"3", "Side", "Description3", "Pending"},
			{"4", "Bottom", "Description4", "Pending"},
		};
		/*for(int i=0; i<ads.size();i++) {
			Ad ad = ads.get(i);
			System.out.println(""+ ad.getID());
			System.out.println((ad.getAdvertisemenetScheme().getPosition() == 0 ? "Bottom" : "Side"));
			System.out.println(ad.getDescrition());
			System.out.println((ad.getApproval() ? "Active" : "Pending"));
		}*/
		return rowsAndColumn;
	
	}
	private void addDummyData() {
		/*ads.add(new Ad(1, null, "Heeeej" , new AdvertisementScheme(1, 12, 30, 500), false));
		ads.add(new Ad(2, null, "Heeeej2" , new AdvertisementScheme(1, 14, 33, 5020), true));
		ads.add(new Ad(3, null, "Heeee3" , new AdvertisementScheme(1, 12, 30, 500), true));
		ads.add(new Ad(4, null, "Heeeej4" , new AdvertisementScheme(1, 52, 123, 340), false));*/
	}

	/*private JPanel initButtonPanel() {
		JPanel btnPanel = new JPanel();
		// Creates buttons
		JButton btnEditAdvertise = new JButton("Edit Advertise");
		JButton btnRemoveAdvertise = new JButton("Remove Advertise");
		JButton btnNewAdvertise = new JButton("New Advertise");

		btnRemoveAdvertise.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println("Remove Ad");
			}
		});
		btnPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		btnNewAdvertise.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				centerPanel.removeAll();
				centerPanel.add(new NewAdvertismenetGUI());
				centerPanel.updateUI();
				centerPanel.repaint();
			}
		});
		btnPanel.add(btnNewAdvertise);
		btnPanel.add(btnEditAdvertise);
		btnPanel.add(btnRemoveAdvertise);
		return btnPanel;
	}*/
	private void addAdvertiserMenu() {
		JMenu mnAdvertiser = new JMenu("Advertiser");	
		/** Menu Items **/
		JMenuItem mntmNewAdvertise = new JMenuItem("New Ad");
		JMenuItem mntmEditAdvertise = new JMenuItem("Edit Ad");
		JMenuItem mntmRemoveAdvertise = new JMenuItem("Remove Ad");
		JMenuItem mntmAccountBalance = new JMenuItem("Account Balance");
		/** Action Listener **/
		mntmNewAdvertise.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				updateJpanel(new NewAdvertismenetGUI());	
			}
		});
		
		mntmEditAdvertise.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				updateJpanel(new EditAdvertisementGUI());		
			}
		});
		
		mntmAccountBalance.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				updateJpanel(new HandleAccountBalanceGUI());		
			}
		});
		
		mntmRemoveAdvertise.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				updateJpanel(new RemoveAdvertisementGUI());
				
			}
		});
		mnAdvertiser.add(mntmNewAdvertise);
		mnAdvertiser.add(mntmEditAdvertise);
		mnAdvertiser.add(mntmRemoveAdvertise);
		mnAdvertiser.add(mntmAccountBalance);
		
		// Add Menu to the main frame
		ArenaManager.getInstance().addMenuItem(mnAdvertiser);
	}
	
	private void updateJpanel(JPanel panel) {
		centerPanel.removeAll();
		centerPanel.add(panel);
		centerPanel.updateUI();
		centerPanel.repaint();
	}
	
}
